import cn.xy.service.CheckUserStatus;
import cn.xy.service.UserInterface;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.regex.Pattern;

/**
 * Created by xiaoyuan on 2018/1/11.
 */
public class Main {
    public static void main(String[] args) {
        /*1、spring方式調用-start*/
        /*
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "META-INF/spring/applicationContext.xml");
        context.start();

         // 获取bean
        CheckUserStatus checkPersonStatus = (CheckUserStatus) context.getBean("checkUserStatus");
        //调用checkPersonStatus，checkPersonStatus调用了dubbo服务
        String allInfo = checkPersonStatus.checkAllPersonStatus();
        String oneInfo = checkPersonStatus.checkPersonStatusByPersonNumber("12345678");
        System.out.println(allInfo);
        System.out.println(oneInfo);
        try {
            Thread.sleep(3600 * 24);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
         //关闭spring的容器
        context.close();
        context.stop();
        */
         /*1、spring方式調用-end*/
        /**
         * 2、纯java方式调用
         */
        // 当前应用配置

        ApplicationConfig application = new ApplicationConfig();
        application.setName("user-center");

        // 连接注册中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://192.168.126.251:2181");

        // 注意：ReferenceConfig为重对象，内部封装了与注册中心的连接，以及与服务提供方的连接

        // 引用远程服务
        ReferenceConfig<UserInterface> reference = new ReferenceConfig<UserInterface>(); // 此实例很重，封装了与注册中心的连接以及与提供者的连接，请自行缓存，否则可能造成内存和连接泄漏
        reference.setApplication(application);
        reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
        reference.setInterface(UserInterface.class);

        // 和本地bean一样使用xxxService
        UserInterface userInterface = reference.get(); // 注意：此代理对象内部封装了所有通讯细节，对象较重，请缓存复用
        String response = userInterface.queryUserInfoByNumber("this is not a spring method invoke");
        System.out.println(response);

    }
}
